/*************************************************************************************************
#  Copyright (C), 20xx-20xx, xxx. Co., Ltd.
#  File Name :		uart_dma.c
#  Author : decorn		Version : V1.0		Date : 2019.04.17
#  Description :
#	uart BSP
#  Others :
#	None
#  Function List :
#	exch_spi_init
#	exch_get_status
#  History :
#	1. Author : shenjh		Version : V1.0		Date : 2019.04.17
#		Modification: Create
#	2.
#************************************************************************************************/

#include "uart_dma.h"


 uint8_t UART1_TXBUFFER[UART_MAXLENGTH];
 uint8_t UART1_RXBUFFER[UART_MAXLENGTH];

void USART1TX_DMA_Init(void)
{
//	uint8_t i=0;
   NVIC_InitTypeDef    NVIC_InitStructure;
   DMA_InitTypeDef DMA_InitStructure;
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
//	  	for(i=0;i<83;i++)
//	{
//	UART1_TXBUFFER[i]=i;
//	}
   SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_USART1Tx,ENABLE);
   DMA_InitStructure.DMA_BufferSize = UART_MAXLENGTH; // 缓存大小
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;    // 内存到内存关闭
   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;   // 普通模式
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  // 内存到外设
   DMA_InitStructure.DMA_Priority = DMA_Priority_High; // DMA通道优先级
   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;// 内存地址递增
   DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)&USART1->TDR; // 外设地址
   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;// 外设地址不变
   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // 内存数据长度
   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)UART1_TXBUFFER;   // 定义内存基地址
   DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;//外设数据长度
   
   DMA_Init(DMA1_Channel4,&DMA_InitStructure);
   DMA_ClearITPendingBit(DMA1_IT_TC4); // 清除一次DMA中断标志
   DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);// 使能DMA传输完成中断
  
   NVIC_InitStructure.NVIC_IRQChannel = (uint8_t) DMA1_Channel4_5_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_InitStructure.NVIC_IRQChannelPriority = 2u;
   NVIC_Init(&NVIC_InitStructure);
   
   DMA_Cmd(DMA1_Channel4,DISABLE);
   
}

void USART1RX_DMA_Init(void)
{
   DMA_InitTypeDef DMA_InitStructure;
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
   SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_USART1Rx,ENABLE);

   DMA_InitStructure.DMA_BufferSize = 83u; // 缓存大小
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;    // 内存到内存关闭
   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;   // 普通模式
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  // 外设到内存
   DMA_InitStructure.DMA_Priority = DMA_Priority_High; // DMA通道优先级
   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;// 内存地址递增

   DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)&USART1->RDR; // 外设地址
   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;// 外设地址不变
   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // 内存数据长度
   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)UART1_RXBUFFER;   // 定义内存基地址
   DMA_InitStructure.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;//外设数据长度
   
   DMA_Init(DMA1_Channel5,&DMA_InitStructure);
   USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); 
  // DMA_ClearITPendingBit(DMA1_IT_TC5); // 清除一次DMA中断标志
  //DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE);// 使能DMA传输完成中断
  
   DMA_Cmd(DMA1_Channel5,ENABLE);
}



/*****************************************
* 配置串口 目前除了波特率其他参数无效
* Usart1 9600-8-N-1
*****************************************/
void USART1_Init( uint32_t ulBaudRate )
{
	GPIO_InitTypeDef		GPIO_InitStructure;
	USART_InitTypeDef		USART_InitStructure;
	NVIC_InitTypeDef        NVIC_InitStructure;	
	/* Enable USART1, PA, AFIO */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG , ENABLE);//一定要开，DMA复用
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);	/* Tx */
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);	/* Rx*/
	GPIO_InitStructure.GPIO_Pin = ((uint32_t)GPIO_Pin_9)|((uint32_t)GPIO_Pin_10);	/* USART1_TX */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configrate USART1 */
	USART_InitStructure.USART_BaudRate = ulBaudRate;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx;//初始化默认不开发送
	USART_Init(USART1, &USART_InitStructure);
	
	USART_ClearFlag(USART1,USART_FLAG_TC);
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);  //开启 USART1 总线空闲中断
	USART_ClearITPendingBit(USART1, USART_IT_IDLE); // 清除空闲中断
	USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
	USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);	
	/* ENABLE the USARTx */
	USART_Cmd(USART1, ENABLE);
	

   NVIC_InitStructure.NVIC_IRQChannel =(uint8_t) USART1_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_InitStructure.NVIC_IRQChannelPriority = 1u;
   NVIC_Init(&NVIC_InitStructure);
	//USART_SendData(USART1, 0xaa);

}

void UART_TX_Enable(void)
{
	
	USART1->CR1&=~(uint32_t)0x01;

	USART1->CR1=(uint32_t)0x1C;
	
	USART1->CR1|=(uint32_t)0x01;
	
}		

void UART_TX_DISABLE(void)
{

	USART1->CR1&=~0x01u;
	
	USART1->CR1=0x14u;
		
	USART1->CR1|=0x01u;

}		
	
void USART1_DMA_SEND(uint16_t num) //DMA_UART_TX发送
{
		UART_TX_Enable();
	  DMA_Cmd(DMA1_Channel4,DISABLE); // 先关掉DMA通道
    DMA_SetCurrDataCounter(DMA1_Channel4,num); // 设置需要发送的长度
    DMA_Cmd(DMA1_Channel4,ENABLE); // 再打开DMA通道
}	

void USART1_DMA_RECEIVE(uint16_t num)
{
    DMA_Cmd(DMA1_Channel5,DISABLE); // 先关掉DMA通道
    DMA_SetCurrDataCounter(DMA1_Channel5,num); // 设置需要接收的长度
    DMA_Cmd(DMA1_Channel5,ENABLE); // 再打开DMA通道
}	

	
